{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "f4TSNCvpENrW"
      },
      "source": [
        "##### Copyright 2019 The TensorFlow Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "form",
        "colab": {},
        "colab_type": "code",
        "id": "vamNSA0vEP-m"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "e1oSi4lHFt3z"
      },
      "source": [
        "# Use XLA with tf.function"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "b7noD9NjFRL-"
      },
      "source": [
        "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n",
        "  \u003ctd\u003e\n",
        "    \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/xla/tutorials/compile\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n",
        "  \u003c/td\u003e\n",
        "  \u003ctd\u003e\n",
        "    \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/compiler/xla/g3doc/tutorials/compile.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
        "  \u003c/td\u003e\n",
        "  \u003ctd\u003e\n",
        "    \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/xla/g3doc/tutorials/compile.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n",
        "  \u003c/td\u003e\n",
        "\u003c/table\u003e"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "sDy5lSBd4BDE"
      },
      "source": [
        "This tutorial trains a TensorFlow model to classify the MNIST dataset, where the training function is compiled using XLA.\n",
        "\n",
        "First, load TensorFlow and enable eager execution."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "45kUPj5ZFrRa"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "tf.compat.v1.enable_eager_execution()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "GZVNiRmTDV-5"
      },
      "source": [
        "Then define some necessary constants and prepare the MNIST dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "f37TSEGvGX4_"
      },
      "outputs": [],
      "source": [
        "# Size of each input image, 28 x 28 pixels\n",
        "IMAGE_SIZE = 28 * 28\n",
        "# Number of distinct number labels, [0..9]\n",
        "NUM_CLASSES = 10\n",
        "# Number of examples in each training batch (step)\n",
        "TRAIN_BATCH_SIZE = 100\n",
        "# Number of training steps to run\n",
        "TRAIN_STEPS = 1000\n",
        "\n",
        "# Loads MNIST dataset.\n",
        "train, test = tf.keras.datasets.mnist.load_data()\n",
        "train_ds = tf.data.Dataset.from_tensor_slices(train).batch(TRAIN_BATCH_SIZE).repeat()\n",
        "\n",
        "# Casting from raw data to the required datatypes.\n",
        "def cast(images, labels):\n",
        "  images = tf.cast(\n",
        "      tf.reshape(images, [-1, IMAGE_SIZE]), tf.float32)\n",
        "  labels = tf.cast(labels, tf.int64)\n",
        "  return (images, labels)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "lv7I-u_82v1S"
      },
      "source": [
        "Finally, define the model and the optimizer. The model uses a single dense layer."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "7O2NcEfG206Q"
      },
      "outputs": [],
      "source": [
        "layer = tf.keras.layers.Dense(NUM_CLASSES)\n",
        "optimizer = tf.keras.optimizers.Adam()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "x_ZehpZP-SfS"
      },
      "source": [
        "# Define the training function\n",
        "\n",
        "In the training function, you get the predicted labels using the layer defined above, and then minimize the gradient of the loss using the optimizer. In order to compile the computation using XLA, place it inside `tf.function` with `experimental_compile=True`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "ZbhJl_WvGa3g"
      },
      "outputs": [],
      "source": [
        "@tf.function(experimental_compile=True)\n",
        "def train_mnist(images, labels):\n",
        "    images, labels = cast(images, labels)\n",
        "\n",
        "    with tf.GradientTape() as tape:\n",
        "      predicted_labels = layer(images)\n",
        "      loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(\n",
        "          logits=predicted_labels, labels=labels\n",
        "      ))\n",
        "    layer_variables = layer.trainable_variables\n",
        "    grads = tape.gradient(loss, layer_variables)\n",
        "    optimizer.apply_gradients(zip(grads, layer_variables))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "EZD1m_n1DxAF"
      },
      "source": [
        "# Train and test the model"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "gukC2Hol3sFZ"
      },
      "source": [
        "Once you have defined the training function, define the model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "qe28bAHNHUG2"
      },
      "outputs": [],
      "source": [
        "for images, labels in train_ds:\n",
        "  if optimizer.iterations \u003e TRAIN_STEPS:\n",
        "    break\n",
        "  train_mnist(images, labels)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "qgsKmz3n2UiW"
      },
      "source": [
        "And, finally, check the accuracy:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "_GxF6jTRHVuA"
      },
      "outputs": [],
      "source": [
        "images, labels = cast(test[0], test[1])\n",
        "predicted_labels = layer(images)\n",
        "correct_prediction = tf.equal(tf.argmax(predicted_labels, 1), labels)\n",
        "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
        "print(\"Prediction accuracy after training: %s\" % accuracy)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [
        "f4TSNCvpENrW"
      ],
      "name": "Use XLA with tf.function",
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.5rc1"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
